<?php

namespace app\app\model\shop;

use app\admin\model\goods\GoodsSkuModel;
use think\Model;

class ShopGoodsModel extends Model
{
    protected $name = 'shop_goods';

    /**
     * Notes: 查询店铺商品
     * User: AerYYY
     * Date: 7/2/25
     * @param $params
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function list($params)
    {
        $where = [];
        $where['sg.shop_id'] = ['=', $params['shop_id']];
        if (!empty($params['category_id'])) {
            $where['g.category_id'] = ['=', $params['category_id']];
        }
        if(!empty($params['goods_name'])){
            $where['g.name|g.en_name'] = ['like', '%' . $params['goods_name'] . '%'];
        }

        $list = $this->alias('sg')->join('goods g', 'g.id = sg.goods_id')
            ->join('goods_category cate', 'cate.id = g.category_id')
            ->where($where)->field('sg.stock,sg.price,g.id,g.name,g.en_name,
            g.image,g.category_id,cate.name cate_name,g.code')->select();

        foreach ($list as &$item) {
            $item['image'] = Https($item['image']);
        }

        return $list;
    }


    /**
     * Notes: 商品详情
     * User: AerYYY
     * Date: 7/2/25
     * @param $params
     * @return array|bool|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($params)
    {
        $where = [];
        $where['sg.shop_id'] = ['=', $params['shop_id']];
        $where['sg.goods_id'] = ['=', $params['id']];

        $info = $this->alias('sg')->join('goods g', 'g.id = sg.goods_id')
            ->join('goods_category cate', 'cate.id = g.category_id')
            ->where($where)->field('sg.stock,sg.price,g.id,g.name,g.en_name,
            g.image,g.category_id,cate.name cate_name,g.code')->find();

        if (empty($info)) {
            $this->error = 'The current product does not exist';
            return false;
        }
        $info['image'] = Https($info['image']);

        return $info;
    }
    /**
     * Notes: 获取商品SKU
     * User: AerYYY
     * Date: 7/2/25
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSku($params)
    {
        $sku_data = GoodsSkuModel::where('goods_id', $params['id'])
            ->group('sku_name')->select();
        $attr = [];
        foreach ($sku_data as &$value) {
            $attr[] = [
                'sku_name' => $value['sku_name'],
                'data' => GoodsSkuModel::where('goods_id', $value['goods_id'])->where('sku_name', $value['sku_name'])
                    ->field('en_name,cn_name,price,id')->select()
            ];
        }

        return $attr;
    }


}